// 异步操作
import { Icon } from "@/api";
import { Menu } from "@/api";

// 加载图标列表 
let icon_list = async (context, payload) => {
    // 图标界面刚打开，payload为空会报错，故后面加{}
    let { pageIndex = 1, pageSize = 30 } = payload || {};
    let { status, data, msg, total } = await Icon.list({ pageIndex, pageSize });
    if (status) {
        context.commit('ICON_LIST', data);
    }
    return { status, msg, total }
}

// 获取选中的图标信息
let select_icon = async (context, payload) => {
    context.commit('SELECT_ICON', payload);
}

// 设置菜单图标
let upload_icon = async (context, payload) => {
    let { icon_id, menu: { menu_id } } = payload;
    let { status, msg } = await Menu.icon(menu_id, { icon_id });
    if (status) {
        let menu_icon = context.state.icons.find(item => item.icon_id === icon_id);
        return { status, msg, menu_icon }
    }
}

export default {
    icon_list,
    select_icon,
    upload_icon,
}